package com.yunqing.restkeeper.cash.web;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;

import java.io.File;

/**
 * @ClassName CashWebApplication
 * @Description TODO
 * @author: puyunqing
 * @Date: 2025/8/21
 */
@EnableDiscoveryClient
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}, scanBasePackages = {"com.yunqing"})
public class CashWebApplication {

    public static void main(String[] args) {

        // 在应用启动前设置Dubbo的本地缓存路径
        SpringApplication application = new SpringApplication(CashWebApplication.class);
        application.addInitializers(new ApplicationContextInitializer<ConfigurableApplicationContext>() {
            @Override
            public void initialize(ConfigurableApplicationContext applicationContext) {
                Environment env = applicationContext.getEnvironment();
                String appName = env.getProperty("spring.application.name"); // 获取应用名
                if (appName == null) {
                    appName = "cash-web"; // 提供一个默认值以防万一
                }
                String userHome = System.getProperty("user.home");
                String dubboCacheDir = userHome + File.separator + ".dubbo" + File.separator + appName;

                // 设置元数据缓存路径
                System.setProperty("dubbo.meta.cache.filePath", dubboCacheDir + File.separator + ".metadata.dubbo.cache");
                // 设置映射缓存路径
                System.setProperty("dubbo.mapping.cache.filePath", dubboCacheDir + File.separator + ".mapping.dubbo.cache");

                // 您也可以为 registry.cache 设置路径，但通常元数据和映射缓存是主要冲突点
                // System.setProperty("dubbo.registry.file", dubboCacheDir + File.separator + "registry.cache");

                System.out.println("Dubbo local cache path set to: " + dubboCacheDir);
            }
        });
        application.run(args);
    }

}
